package com.cy.mongodb;

import com.cy.mongodb.entity.User;
import com.cy.mongodb.repository.UserRepository;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;

import java.util.List;

//import org.springframework.data.mongodb.core.query.Criteria;

@SpringBootTest
public class MongodbApplicationTest1 {

    /**
     * 基于 mongoRepository 开发 增删改查（CRUD）
     */
    //注入mongoRepository
    @Autowired
    private UserRepository userRepository;

    //   mongodb://admin:123456@localhost:27017/test
    //认证授权 用户名 密码  本机  端口   数据库

    //添加操作
    @Test
    public void create() {
        User user = new User();
        user.setAge(22);
        user.setName("zahgnsne");
        user.setEmail("sfsfs@163.com");
        User save = userRepository.save(user);
        System.out.println(
                save
        );

    }

    //查询所有记录
    @Test
    public void findAll() {
        List<User> all = userRepository.findAll();
        System.out.println(all);

    }

    //id查询
    @Test
    public void findId() {
        User user = userRepository.findById("61834578403e1820ee7a8d1b").get();
        System.out.println(user);

    }

    //条件查询
    @Test
    public void findUserList() {
        //name=张某  and age=20
        User user = new User();
        user.setAge(20);
        user.setName("张某");
        Example<User> userException = Example.of(user);
        List<User> all = userRepository.findAll(userException);
        System.out.println(all);

    }

    //模糊查询
    @Test
    public void findLikeUserList() {
        //设置模糊查询匹配规则
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //设置模糊查询
                .withIgnoreCase(true);     //忽略大小写
        User user = new User();
        user.setAge(20);
        user.setName("张");
        Example<User> userException = Example.of(user, matcher);
        List<User> all = userRepository.findAll(userException);
        System.out.println(all);

    }

    //分页查询
    @Test
    public void findPageUserList() {
        //设置分页参数
        //0代表第一页
        PageRequest pageable = PageRequest.of(0, 3);

        User user = new User();
        user.setAge(20);
        user.setName("张某");
        Example<User> userException = Example.of(user);
        Page<User> all = userRepository.findAll(userException, pageable);
        System.out.println(all);
    }


    //修改
    @Test
    public void updateUser() {
        //有id修改 没id新增
        User user = userRepository.findById("61834578403e1820ee7a8d1b").get();
        user.setAge(100);
        user.setName("User_11");
        user.setEmail("1011@qq.com");
        User save = userRepository.save(user);
        System.out.println(save);

    }

    //删除
    @Test

    public void deleteUser() {
        userRepository.deleteById("61834578403e1820ee7a8d1b");
    }
}

